--- title: 'Creating maps with ''Leaflet''' author: admin date: '2020-09-01' slug: Coursera-Leaflet categories: - R - Spatial Analysis tags: [R,Spatial Analysis, Yucatan Peninsula] subtitle: 'Part of Course: Developing Data Products (Coursera - Johns Hopkins)' summary: 'This is a exercise for Coursera and Johns Hopkins Specialization in Data Science' authors: [admin] lastmod: '2020-09-01T16:00:13-05:00' featured: no image: caption: '' focal_point: '' preview_only: no projects: [] ---
This is a exercise for Coursera and Johns Hopkins Specialization in Data Science. Here is a map created with ‘Leaflet’ package. Also, I used ‘dplyr’ and for cleaning the data.
This map shows Water allocations in Yucatán Península, México according to REPDA by CONAGUA, September 2020. Coordinates are not validate ### First step: Download and data cleaning
library(leaflet)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
url <-"http://201.116.60.46/DatosAbiertos/Otorgamiento_de_Concesiones.zip"
temp <- tempfile()
temp2 <- tempfile()
download.file(url, temp, mode="wb")
unzip(zipfile = temp, exdir = temp2)
data <- read.csv(file = paste0(temp2,"\\CONCESIONES","\\ANEXOS_SUBTERRANEOS.csv",sep=""),encoding = "UTF-8")
data1 <- read.csv(file = paste0(temp2,"\\CONCESIONES","\\CONCESIONES.csv",sep=""),encoding = "UTF-8")
data$lat=data$GRADOS.LATITUD+((1/60)*data$MINUTOS.LATITUD)+((1/3600)*data$SEGUNDOS.LATITUD)
data$lng=((data$GRADOS.LONGITUD)+((1/60)*data$MINUTOS.LONGITUD)+((1/3600)*data$SEGUNDOS.LONGITUD))*-1
data_all <- merge(x = data,y= data1, by = "X.U.FEFF.TITULO", all.x = TRUE)
df <- filter(data_all, NOMBRE.DE.ESTADO %in% c("CAMPECHE","QUINTANA ROO","YUCATAN"),lat>=17.5,lat<=22,lng<=-87,lng>=-92) #filter by State and coordinates
df <- data.frame(lat=c(df$lat),lng=c(df$lng),uso=c(df$USO.QUE.AMPARA.EL.TITULO),vol=df$VOLUMEN.ANUAL.EN.m3)
unlink(c(temp, temp2))
pal <- colorFactor(c("cyan3","darkgreen","sienna","grey77","dodgerblue4",
"red4","tan4","darkslategrey","gold3"),df$uso) #Set colors
df %>%
leaflet() %>%
addTiles() %>% # It generate the main layer
addMarkers(popup = paste(df$uso,df$vol,"m3/year"),clusterOptions = markerClusterOptions()) %>% # Define marks
addCircles(weight = 1, radius = sqrt(df$vol) * 2,color=pal(df$uso)) %>%
addLegend(pal = pal,values = df$uso)
## Assuming "lng" and "lat" are longitude and latitude, respectively
## Assuming "lng" and "lat" are longitude and latitude, respectively
Clusters visualize number of allocation in the region, circle describe each concession by type and volume